{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## `Similarity Search` using `NumPy!` 🚀 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Index of the nearest tweet: 0\n",
      "Embedding of the nearest tweet: [1 2 3]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "\n",
    "# Query tweet\n",
    "query_tweet = np.array([1, 2, 3])\n",
    "\n",
    "# A database of all the tweets stored in form of embeddings\n",
    "vector_database = np.array([[1, 2, 3],\n",
    "                            [4, 5, 6],\n",
    "                            [7, 8, 9],\n",
    "                            [2, 3, 4],\n",
    "                            [5, 6, 7]])\n",
    "\n",
    "\n",
    "\n",
    "# Normalize the matrix M and the target vector v\n",
    "vector_database_norm = vector_database / np.linalg.norm(vector_database, axis=1, keepdims=True)\n",
    "query_tweet_norm = query_tweet / np.linalg.norm(query_tweet)\n",
    "\n",
    "# Compute the dot product of the normalized matrix M and the normalized target vector v\n",
    "dot_product = np.dot(vector_database_norm, query_tweet_norm)\n",
    "\n",
    "# Find the index of the nearest to query tweet in the vector data base\n",
    "nearest_vector_index = np.argmax(dot_product)\n",
    "\n",
    "print(\"Index of the nearest tweet:\", nearest_vector_index)\n",
    "print(\"Embedding of the nearest tweet:\", vector_database[nearest_vector_index])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "env_twitter",
   "language": "python",
   "name": "env_twitter"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
